#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/4/12  13:14
# @Author: 余浪人
# @email: yulangren520@gmail.com
from flask_login import login_required
from apps.cms import cms_bp
from flask import render_template, request, jsonify
from apps.forms.column_form import Column_Form, Column_Edit_Form
from apps.models.column_model import Column_model, db
from apps.lib.public import check_obj, fake_delete_data, real_delete_data, recover_data, update_status, fake_deletes, \
    real_deletes, log, page_auto
from apps.lib.public import add_data, edit_data
from apps.forms.document_form import Document_Form
from apps.models.document_model import Document_Model


# 栏目分类列表
@cms_bp.route('/column_list', endpoint='column_list', methods=['GET', 'DELETE', 'POST', 'DELETE_S'])
@login_required
def column_list():
    uid = request.form.get('uid')
    if request.method == 'DELETE':
        if Column_model.query.filter(Column_model.column_parent_id == uid).count():
            return jsonify({"status": 1, "message": "请先移除子栏目!(含:回收站)"})
        elif Document_Model.query.filter(Document_Model.superior==uid).count() :
            return jsonify({"status": 1, "message": "请先移除栏目档案!(含:回收站)"})
        return fake_delete_data(Column_model, db, uid)
    elif request.method == 'DELETE_S':
        return fake_deletes(Column_model, db, uid_s=list(eval(request.form.get('data_s') + ',')))
    elif request.method == 'POST':
        return update_status(Column_model, db, uid, request.form.get('status'))  # 启用与停用
    seed = request.args.get('seed')
    data_s = Column_model.query.order_by(db.desc(Column_model.id)).filter(Column_model.status != 0,
                                                                          Column_model.column_parent_id == 0)
    if seed:
        data_s = Column_model.query.order_by(db.desc(Column_model.id)).filter(Column_model.status != 0,
                                                                              Column_model.column_parent_id == seed)
    paginate, page = page_auto(data_s)
    return render_template('sys_templates/column_list.html', title='栏目列表',url='cms_bp.column_list', data_s=paginate, page=page)


# 栏目分类添加
@cms_bp.route('/column_add', endpoint='column_add', methods=['GET', 'POST'])
@login_required
def column_add():
    if request.method == 'POST':
        log('操作添加分类')
        return add_data(Column_model, Column_Form, db)
    form = Column_Form(request.form)
    return render_template('sys_templates/column.html', form=form, title='分类栏目添加')


# 栏目分类修改
@cms_bp.route('/column_edit/<int:id>', endpoint='column_edit', methods=['GET', 'POST'])
@login_required
def column_edit(id):
    if request.method == 'POST':
        log('操作修改分类')
        return edit_data(Column_Edit_Form, Column_model, db, id)
    obj = Column_model.query.filter_by(id=id, status=1).first()
    if not check_obj(obj):
        form = Column_Edit_Form(data=dict(obj))
        return render_template('sys_templates/column.html', form=form, title='分类栏目修改')


# 栏目分类回收站
@cms_bp.route('/column_recycle', endpoint='column_recycle', methods=['GET', 'DELETE', "UPDATE"])
@login_required
def column_recycle():
    if request.method == 'DELETE':
        log('操作删除分类')
        return real_delete_data(Column_model, db, request.form.get('uid'))
    elif request.method == 'UPDATE':
        return recover_data(Column_model, db, request.form.get('uid'))
    data_s = Column_model.query.order_by(db.desc(Column_model.id)).filter(Column_model.status == 0)
    paginate, page = page_auto(data_s)
    return render_template('sys_templates/column_list.html', data_s=paginate, page=page, url='cms_bp.column_recycle', title='已删除栏目分类')


# 文档列表
@cms_bp.route('/document_list', endpoint='document_list', methods=['GET', 'DELETE', 'DELETE_S'])
@login_required
def document_list():
    if request.method == 'DELETE':
        return fake_delete_data(Document_Model, db, request.form.get('uid'))
    elif request.method == 'DELETE_S':
        uid_s = list(eval(request.form.get('data_s') + ','))
        return fake_deletes(Document_Model, db, uid_s)
    data_s = Document_Model.query.order_by(db.desc(Document_Model.id)).filter(Document_Model.status != 0)
    paginate, page = page_auto(data_s)
    return render_template('sys_templates/document_list.html', data_s=paginate, page=page,url='cms_bp.document_list', title='文档列表')


# 文档添加
@cms_bp.route('/document_add', endpoint='document_add', methods=['GET', 'POST'])
@login_required
def document_add():
    if request.method == 'POST':
        return add_data(Document_Model, Document_Form, db)
    form = Document_Form(request.form)
    return render_template('sys_templates/document.html', form=form, title='新建文档')


# 文档修改
@cms_bp.route('/document_edit/<int:id>', endpoint='document_edit', methods=['GET', 'POST'])
@login_required
def document_edit(id):
    if request.method == 'POST':
        return edit_data(Document_Form, Document_Model, db, id)
    obj = Document_Model.query.filter_by(id=id).first()
    if not check_obj(obj):
        form = Document_Form(data=dict(obj))
        return render_template('sys_templates/document.html', form=form, title=f'修改{obj.title}')


# 文档回收站
@cms_bp.route('/document_recycle', endpoint='document_recycle', methods=['GET', 'DELETE', 'DELETE_S', 'UPDATE'])
@login_required
def document_recycle():
    if request.method == 'DELETE':
        return real_delete_data(Document_Model, db, request.form.get('uid'))
    elif request.method == 'DELETE_S':
        uid_s = list(eval(request.form.get('data_s') + ','))
        return real_deletes(Document_Model, db, uid_s)
    elif request.method == 'UPDATE':
        return recover_data(Document_Model, db, request.form.get('uid'))
    data_s = Document_Model.query.order_by(db.desc(Document_Model.id)).filter(Document_Model.status == 0)
    paginate, page = page_auto(data_s)
    return render_template('sys_templates/document_list.html', data_s=paginate, page=page,url='cms_bp.document_recycle', title='文档回收站')
